package com.ruide_info.demo.service.demo1;

import com.ruide_info.demo.model.demo1.Demo1RequestBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import ruide_info_mvc.base.core.check.IBaseCheckParams;
import ruide_info_mvc.base.core.check.Params;
import ruide_info_mvc.base.core.exception.customException.CommonException;
import ruide_info_mvc.base.model.response.BaseResponseBean;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Demo1Service
 * @Author wangBo
 * @Date 2019/7/21 0021 下午 3:19
 * @Version 1.0
 */
@Service(value = "demo1Service")
public class Demo1Service implements IDemo1Service {

    private final Logger logger = LoggerFactory.getLogger(Demo1Service.class);

    @Resource(name = "baseCheckParams")
    private IBaseCheckParams<Demo1RequestBean> checkParams;

    @Override
    public BaseResponseBean insertDemo(Demo1RequestBean demo1RequestBean)  throws Exception{
        // key : bean的字段, value : 配置文件的属性
        checkParams.checkParams(demo1RequestBean , Params.build().putCheckParams("demo" , "demo.error").putCheckParams("length" , "length.error"));
        // 模拟dao
        try {
//        XXDao.insertDemo(demo1RequestBean);
        } catch (Exception e) {
            // 读配置文件的配置属性
            throw new CommonException("insert.error" , true);
            // 不会读配置文件的属性
//            throw new CommonException("新增失败" , false);
        }
        return BaseResponseBean.buildSeccsec("新增成功!" , "{result: true}");
    }

    @Override
    public BaseResponseBean deleteDemo(Demo1RequestBean demo1RequestBean) throws Exception {
        // key : bean的字段, value : 配置文件的属性
        checkParams.checkParams(demo1RequestBean , Params.build().putCheckParams("demo" , ""));
        // 模拟dao
        try {
//        XXDao.deleteDemo(demo1RequestBean);
        } catch (Exception e) {
            // 读配置文件的配置属性
            throw new CommonException("delete.error" , true);
            // 不会读配置文件的属性
//            throw new CommonException("删除失败" , false);
        }
        return BaseResponseBean.buildSeccsec("新增成功!" , "{result: true}");
    }

    @Override
    public BaseResponseBean queryDemoLIst(Demo1RequestBean demo1RequestBean) throws Exception {
        // key : bean的字段, value : 配置文件的属性
        checkParams.checkParams(demo1RequestBean , Params.build().putCheckParams("demo" , ""));
        List<Object> result;
        try {
            result = new ArrayList<Object>(10);
            for (int i = 0, count = 10; i < count; i++) {
                result.add(i);
            }
        } catch (Exception e) {
            // 读配置文件的配置属性
            throw new CommonException("query.error" , true);
            // 不会读配置文件的属性
//            throw new CommonException("删除失败" , false);
        }
        return BaseResponseBean.buildSeccsec("新增成功!" , result);
    }

    @Override
    public BaseResponseBean uploadPic(Demo1RequestBean demo1RequestBean, MultipartFile[] files) throws Exception {
        // 上传图片
        logger.info(" demo1RequestBean : {} , files : {} " ,demo1RequestBean , files[0].getOriginalFilename());
        // 业务处理
        return  BaseResponseBean.buildSeccsec("上传成功!" , "{result: true}");
    }
}
